package com.yanqu.road.server.manger.activity.hsisland;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.activity.hsisland.HsIslandConfigDaoImpl;
import com.yanqu.road.dao.impl.activity.sonhai.SonHaiConfigDaoImpl;
import com.yanqu.road.dao.impl.player.UserPatronsDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.hsisland.config.*;
import com.yanqu.road.entity.activity.sonhai.config.*;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiVipPowerData;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiEnergyPropEnum;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.goodsLimit.GoodsLimitModule;
import com.yanqu.road.server.gameplayer.module.activity.hsisland.HsIslandActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.sonhai.SonHaiActivityModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HsIslandActivityMgr extends TempMgr {

    /**
     * logger
     */
    private static Logger logger = LogManager.getLogger(HsIslandActivityMgr.class.getName());

    /**
     * 活动配置Map
     */
    private static Map<Integer, HsIslandConfig> activityConfigMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static HsIslandConfig getActivityConfig(int activityId) {
        return activityConfigMap.get(activityId);
    }

    public static Map<Integer, HsIslandConfig> getActivityConfigMap() {
        return activityConfigMap;
    }

    /**
     * 加载活动
     */
    public static void reloadActivityData() {
        getLogger().info("reload HsIslandActivityMgr start");
        //获取开启中的活动
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.HsIsland.getValue());
        HsIslandConfigDaoImpl dao = new HsIslandConfigDaoImpl();
        //创建活动配置Map
        Map<Integer, HsIslandConfig> hsIslandConfigMap = new ConcurrentHashMap<>();
        for (ActivityInfo activityInfo : list) {
            //获取活动config
            Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
            //mall map
            Map<Integer, MallInfo> mallInfoMap = NormalActivityMgr.getActivityMallInfoMap(activityInfo.getActivityId());
            //船配置表
            Map<Integer, HsIslandBoatConfig> boatConfigMap = dao.getBoatConfigMap(activityInfo.getActivityId());
            //岛配置
            Map<Integer, HsIslandIslandConfig> islandConfigMap = dao.getIslandConfigMap(activityInfo.getActivityId());
            //事件配置表
            Map<Integer, HsIslandEventConfig> eventConfigMap = dao.getEventConfigMap(activityInfo.getActivityId());
            //PVE配置表
            Map<Integer, HsIslandPveConfig> pveConfigMap = dao.getPveConfigMap(activityInfo.getActivityId());
            //PVP结算配置MAP
            Map<Integer, HsIslandDuelConfig> duelConfigMap = dao.getDuleConfigMap(activityInfo.getActivityId());
            //宝箱配置MAP
            Map<Integer, HsIslandBoxConfig> boxConfigMap = dao.getBoxConfigMap(activityInfo.getActivityId());
            //2选1配置MAP
            Map<Integer, HsIslandChooseConfig> chooseConfigMap = dao.getChooseConfigMap(activityInfo.getActivityId());
            //BUFF配置MAP
            Map<Integer, HsIslandBuffConfig> buffConfigMap = dao.getBuffConfigMap(activityInfo.getActivityId());
            //遗迹藏宝配置MAP
            Map<Integer, HsIslandRelicConfig> relicConfigMap = dao.getRelicConfigMap(activityInfo.getActivityId());
            //BOSS奖励配置MAP
            Map<Integer, HsIslandBossRewardConfig> bossRewardConfigMap = dao.getBossRewardConfigMap(activityInfo.getActivityId());
            //圣柱奖励配置MAP
            Map<Integer, HsIslandGiftConfig> giftConfigMap = dao.getGiftConfigMap(activityInfo.getActivityId());
            //创建活动config对象
            HsIslandConfig hsIslandConfig = new HsIslandConfig(activityInfo, configMap, mallInfoMap, boatConfigMap, islandConfigMap, eventConfigMap, pveConfigMap, duelConfigMap, boxConfigMap, chooseConfigMap, buffConfigMap, relicConfigMap, bossRewardConfigMap, giftConfigMap);
            hsIslandConfigMap.put(activityInfo.getActivityId(), hsIslandConfig);
        }
        activityConfigMap = hsIslandConfigMap;
        //在线玩家,同步下活动数据
        List<GamePlayer> gamePlayerList = GamePlayerMgr.getAllOnlinePlayer();
        for (GamePlayer gamePlayer : gamePlayerList) {
            //同步活动配置
            gamePlayer.getModule(HsIslandActivityModule.class).syncConfig();
        }
        getLogger().info("reload HsIslandActivityMgr end");
    }

    /**
     * 公用check
     * @param gamePlayer
     * @param activityId
     * @param checkInTime
     * @throws BusinessException
     */
    private static void commonCheck(GamePlayer gamePlayer, int activityId, boolean checkInTime) throws BusinessException {
        //活动是否存在
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityId);
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        //活动配置
        HsIslandConfig config = activityConfigMap.get(activityId);
        if (config == null) {
            throw new BusinessException(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_FOUND);
        }
        //判断是否在活动展示期
        if (!ActivityHelper.activityInShowTime(activityInfo)) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_IN_SHOW_TIME);
        }
        //判断是否在活动进行期
        if (checkInTime && !ActivityHelper.activityInTime(activityInfo)) {
            throw BusinessException.newException(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_IN_TIME);
        }
        //系统是否解锁
        if (!SystemOpenMgr.checkSystemOpen(gamePlayer.getUserId(), eSystemId.HsIsland.getValue())) {
            throw BusinessException.newException(GameErrorCode.E_SYSTEM_NO_OPEN);
        }
    }
}
